<?php
/**
 * Created by PhpStorm.
 * User: TheLuther
 * Date: 2019/10/28
 * Time: 20:17
 */

namespace app\common\model;

use think\Facade\Request;
use think\Facade\Session;
use think\Model;
use think\Facade;
use think\Db;

class User extends model
{
    protected static $instance;
    public static function instance($options = [])
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($options);
        }
        return self::$instance;
    }

    public function isLogin(){
        $user = Session::get('admin_user');
        if ( empty($user) ) {
            return 0;
        } else {
            $data = array();
            $data['username'] = $user['username'];
            $data['password'] = $user['password'];
            $data['lastip'] = Request::instance()->ip();
            $sign = $this->getLoginSign($data);
            return Session::get('admin_user_sign') == $sign ? $user['id'] : 0;
        }
    }

    public function getLoginUser($force=false){
        if($this->isLogin())
        {
            $model = Session::get('admin_user');
            if($force)
                $model = Db::name('user')->where(['id'=>$model['id']])->findOrEmpty();
            return $model;
        }else{
            return false;
        }
    }

    public function getLoginSign($data){
        $key = '';
        foreach($data as $v)
        {
            $key .= $v;
        }
        return md5($key);
    }

    public function logout(){
        Session::set('admin_user_sign',null);
        Session::set('admin_user',null);
    }

    public function login($data){
        if(empty($data['username']) || empty($data['password']))
            return false;

        $where = array();
        $where['username'] = $data['username'];

        $usermodel = $this->where(['username'=>$data['username']])->findOrEmpty();
        if(!$usermodel)
            return false;

        if( md5($data['password'].$usermodel['salt']) == $usermodel['password']){
            $lastip = Request::instance()->ip();
            $lasttime = time();
            $usermodel['lastip'] = $lastip;
            $usermodel['lasttime'] = $lasttime;

            $update['lastip'] = Request::instance()->ip();
            $update['lasttime'] = time();
            Db::name('user')->where(['id'=>$usermodel['id']])->update($update);

            $sign['username'] = $usermodel['username'];
            $sign['password'] = $usermodel['password'];
            $sign['lastip'] = Request::instance()->ip();
            $sign = $this->getLoginSign($sign);
            Session::set('admin_user_sign',$sign);
            Session::set('admin_user',$usermodel);
            return true;
        }
        else{
            return false;
        }
    }

    public function getUserGroups($uid){
        return Db::name('user_group')->where(['uid'=>$uid])->select();
    }

    public function setUserGroups($uid,$gids){
        if(!is_array($gids))
            $gids = explode(',',$gids);
        Db::name('user_group')->where(['uid'=>$uid])->delete();
        foreach($gids as $gid){
            if($gid)
                Db::name('user_group')->insert(['uid'=>$uid,'gid'=>$gid]);
        }
        return true;
    }

    public function removeUserFromGroup($gid,$uid){
        Db::name('user_group')->where(['uid'=>$uid,'gid'=>$gid])->delete();
        return true;
    }

    public function getGroupUsers($gid,$page,$pagesize=20){
        $list = Db::name('user')->alias('u')->join('user_group g','u.id=g.uid')->where(['gid'=>$gid])->field('u.*')->page($page,$pagesize)->select();//->paginate($pagesize,false,['page'=>$page]);
        return $list;
    }
}